package com.itsu.controller;

import com.github.pagehelper.PageInfo;
import com.itsu.domain.Catalog;
import com.itsu.domain.QuestionItem;
import com.itsu.service.Impl.QuestionItemServiceImpl;
import com.itsu.service.QuestionItemService;
import com.itsu.utils.BeanUtil;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@WebServlet("/store/questionItem")
public class QuestionItemController extends HttpServlet {

    private QuestionItemService questionItemService = new QuestionItemServiceImpl();

    private String setSaveOrUp = "save";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if ("list".equals(operation)){
            showList(req, resp, null);
        } else if ("toAdd".equals(operation)) {
            toAdd(req, resp);
        } else if ("save".equals(operation)) {
            save(req, resp);
        } else if ("toEdit".equals(operation)) {
            toEdit(req, resp);
        } else if ("edit".equals(operation)) {
            edit(req, resp);
        } else if ("delete".equals(operation)) {
            delete(req, resp);
        }
    }

    private void showList(HttpServletRequest req, HttpServletResponse resp, String questionId) throws ServletException, IOException {
        if (StringUtils.isNullOrEmpty(questionId)) {
            questionId= req.getParameter("questionId");
        }
        int pageNum = 1;
        int pageSize = 5;
        if (!StringUtils.isNullOrEmpty(req.getParameter("page"))){
            pageNum = Integer.parseInt(req.getParameter("page"));
        }
        PageInfo<QuestionItem> pageInfo = questionItemService.findByQuestionId(pageNum, pageSize, questionId);

        req.setAttribute("SaveOrUp", setSaveOrUp);
        req.setAttribute("questionId", questionId);
        req.setAttribute("page", pageInfo);

        req.getRequestDispatcher("/WEB-INF/pages/store/questionItem/list.jsp").forward(req, resp);
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) {
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        QuestionItem questionItem = BeanUtil.fillBean(req, QuestionItem.class);
        questionItem.setId(UUID.randomUUID().toString());
        questionItemService.save(questionItem);

        resp.sendRedirect(req.getContextPath()+ "/store/questionItem?operation=list&questionId=" + questionItem.getQuestionId());
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        QuestionItem questionItem = questionItemService.findById(id);
        setSaveOrUp = "edit";

        req.setAttribute("questionItem", questionItem);
        showList(req, resp, questionItem.getQuestionId());
//        req.getRequestDispatcher("/WEB-INF/pages/store/questionItem/list.jsp").forward(req, resp);
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        QuestionItem questionItem = BeanUtil.fillBean(req, QuestionItem.class);
        questionItemService.update(questionItem);
        setSaveOrUp = "save";
        showList(req, resp, questionItem.getQuestionId());
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        QuestionItem questionItem = BeanUtil.fillBean(req, QuestionItem.class);
        questionItemService.delete(questionItem);
        showList(req, resp, questionItem.getQuestionId());
    }
}
